package com.njtoyo.taxi.admin.service.business.operation_rule.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.njtoyo.taxi.admin.mapper.master.entity.DriverMarkScoreRule;
import com.njtoyo.taxi.admin.mapper.master.enums.DriverMarkScoreRuleCategory;
import com.njtoyo.taxi.admin.mapper.master.mapper.DriverMarkScoreRuleMapper;
import com.njtoyo.taxi.admin.rest.presenter.business.operation_rule.DriverMarkScoreRulePresenter;
import com.njtoyo.taxi.admin.rest.wrapper.business.operation_rule.DriverMarkScoreRuleWrapper;
import com.njtoyo.taxi.admin.service.business.operation_rule.DriverMarkScoreRuleService;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class DriverMarkScoreRuleServiceImpl implements DriverMarkScoreRuleService {
    @Autowired
    private DriverMarkScoreRuleMapper driverMarkScoreRuleMapper;


    @Override
    public RestResult<List<DriverMarkScoreRulePresenter>> getPageList(List<Integer> categorys) {
        if (CollUtil.isEmpty(categorys)) {
            //TODO
        }
        List<DriverMarkScoreRulePresenter> presenters = new ArrayList<>();
        List<DriverMarkScoreRule> rules = driverMarkScoreRuleMapper.getListByCategorys(categorys);
        for (DriverMarkScoreRule rule : rules) {
            DriverMarkScoreRulePresenter presenter = new DriverMarkScoreRulePresenter();
            presenter.setId(rule.getId());
            presenter.setCategoryName(rule.getCategory().getDesc());
            if (rule.getCategory() == DriverMarkScoreRuleCategory.judge) {
                presenter.setEntity(JSON.parseArray(rule.getDetail()));
            } else {
                presenter.setEntity(rule.getDetail());
            }
            presenters.add(presenter);
            System.out.println(presenter);
        }
        return RestResult.success(presenters);
    }

    @Override
    public RestResult update(DriverMarkScoreRuleWrapper wrapper) {
        DriverMarkScoreRule driverMarkScoreRule = driverMarkScoreRuleMapper.selectById(wrapper.getId());
        if (ObjectUtil.isNull(driverMarkScoreRule)) {
            RestResult.failed("数据不存在");
        }
        driverMarkScoreRule.setDetail(wrapper.getDetails());
        driverMarkScoreRuleMapper.updateById(driverMarkScoreRule);
        return RestResult.success("规则更新成功");
    }
}
